﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MCSFramework.SQLDAL;
using MCSFramework.Model.CM;
using MCSFramework.Common;

namespace MCSFramework.SQLDAL.CM
{
    public class CM_ClientDAL : BaseSimpleDAL<CM_Client>
    {
        public CM_ClientDAL()
        {
            _ProcePrefix = "MCS_CM.dbo.sp_CM_Client";
        }

        protected override CM_Client FillModel(System.Data.IDataReader dr)
        {
            CM_Client m = new CM_Client();

            if (!string.IsNullOrEmpty(dr["ID"].ToString())) m.ID = (int)dr["ID"];
            if (!string.IsNullOrEmpty(dr["Code"].ToString())) m.Code = (string)dr["Code"];
            if (!string.IsNullOrEmpty(dr["FullName"].ToString())) m.FullName = (string)dr["FullName"];
            if (!string.IsNullOrEmpty(dr["ShortName"].ToString())) m.ShortName = (string)dr["ShortName"];
            if (!string.IsNullOrEmpty(dr["OrganizeCity"].ToString())) m.OrganizeCity = (int)dr["OrganizeCity"];
            if (!string.IsNullOrEmpty(dr["OfficalCity"].ToString())) m.OfficalCity = (int)dr["OfficalCity"];
            if (!string.IsNullOrEmpty(dr["Address"].ToString())) m.Address = (string)dr["Address"];
            if (!string.IsNullOrEmpty(dr["TeleNum"].ToString())) m.TeleNum = (string)dr["TeleNum"];
            if (!string.IsNullOrEmpty(dr["Fax"].ToString())) m.Fax = (string)dr["Fax"];
            if (!string.IsNullOrEmpty(dr["Email"].ToString())) m.Email = (string)dr["Email"];
            if (!string.IsNullOrEmpty(dr["Url"].ToString())) m.Url = (string)dr["Url"];
            if (!string.IsNullOrEmpty(dr["Postcode"].ToString())) m.PostCode = (string)dr["Postcode"];
            if (!string.IsNullOrEmpty(dr["ChiefLinkMan"].ToString())) m.ChiefLinkMan = (int)dr["ChiefLinkMan"];
            if (!string.IsNullOrEmpty(dr["ActiveFlag"].ToString())) m.ActiveFlag = (int)dr["ActiveFlag"];
            if (!string.IsNullOrEmpty(dr["OpenTime"].ToString())) m.OpenTime = (DateTime)dr["OpenTime"];
            if (!string.IsNullOrEmpty(dr["CloseTime"].ToString())) m.CloseTime = (DateTime)dr["CloseTime"];
            if (!string.IsNullOrEmpty(dr["ClientType"].ToString())) m.ClientType = (int)dr["ClientType"];
            if (!string.IsNullOrEmpty(dr["Supplier"].ToString())) m.Supplier = (int)dr["Supplier"];
            if (!string.IsNullOrEmpty(dr["ClientManager"].ToString())) m.ClientManager = (int)dr["ClientManager"];
            if (!string.IsNullOrEmpty(dr["ApproveFlag"].ToString())) m.ApproveFlag = (int)dr["ApproveFlag"];
            if (!string.IsNullOrEmpty(dr["InsertTime"].ToString())) m.InsertTime = (DateTime)dr["InsertTime"];
            if (!string.IsNullOrEmpty(dr["InsertStaff"].ToString())) m.InsertStaff = (int)dr["InsertStaff"];
            if (!string.IsNullOrEmpty(dr["UpdateTime"].ToString())) m.UpdateTime = (DateTime)dr["UpdateTime"];
            if (!string.IsNullOrEmpty(dr["UpdateStaff"].ToString())) m.UpdateStaff = (int)dr["UpdateStaff"];
            if (!string.IsNullOrEmpty(dr["ExtPropertys"].ToString())) m.ExtPropertys = (string)dr["ExtPropertys"];

            return m;

        }

        public override int Add(CM_Client m)
        {
            //此处参数不设置id字段
            #region 设置参数集

            SqlParameter[] patameters = { 
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("ID",SqlDbType.Int,4,m.ID),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Code",SqlDbType.VarChar,20,m.Code),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("FullName",SqlDbType.VarChar,20,m.FullName),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ShortName",SqlDbType.VarChar,20,m.ShortName),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("OrganizeCity",SqlDbType.Int,4,m.OrganizeCity),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("OfficalCity",SqlDbType.Int,4,m.OfficalCity),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Address",SqlDbType.VarChar,20,m.Address),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("TeleNum",SqlDbType.VarChar,20,m.TeleNum),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Fax",SqlDbType.VarChar,20,m.Fax),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Email",SqlDbType.VarChar,20,m.Email),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Url",SqlDbType.VarChar,20,m.Url),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Postcode",SqlDbType.VarChar,20,m.PostCode),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ChiefLinkMan",SqlDbType.Int,4,m.ChiefLinkMan),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ActiveFlag",SqlDbType.Int,4,m.ActiveFlag),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("OpenTime",SqlDbType.DateTime,8,m.OpenTime),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("CloseTime",SqlDbType.DateTime,8,m.CloseTime),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ClientType",SqlDbType.Int,4,m.ClientType),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("Supplier",SqlDbType.Int,4,m.Supplier),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ClientManager",SqlDbType.Int,4,m.ClientManager),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ApproveFlag",SqlDbType.Int,4,m.ApproveFlag),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("InsertTime",SqlDbType.DateTime,8,m.InsertTime),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("InsertStaff",SqlDbType.Int,4,m.InsertStaff),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("UpdateTime",SqlDbType.DateTime,8,m.UpdateTime),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("UpdateStaff",SqlDbType.Int,4,m.UpdateStaff),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("ExtPropertys",SqlDbType.VarChar,8000,m.ExtPropertys),
        
                                        };
            #endregion
            m.ID = MCSFramework.DBUtility.SQLDatabase.RunProc(_ProcePrefix + "_Add", patameters);
            return m.ID;
        }

        public override int Update(CM_Client m)
        {
            #region 设置参数集
            SqlParameter[] patameters = { 
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ID",SqlDbType.Int,4,m.ID),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Code",SqlDbType.VarChar,20,m.Code),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@FullName",SqlDbType.VarChar,20,m.FullName),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ShortName",SqlDbType.VarChar,20,m.ShortName),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@OrganizeCity",SqlDbType.Int,4,m.OrganizeCity),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@OfficalCity",SqlDbType.Int,4,m.OfficalCity),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Address",SqlDbType.VarChar,20,m.Address),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@TeleNum",SqlDbType.VarChar,20,m.TeleNum),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Fax",SqlDbType.VarChar,20,m.Fax),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Email",SqlDbType.VarChar,20,m.Email),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Url",SqlDbType.VarChar,20,m.Url),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Postcode",SqlDbType.VarChar,20,m.PostCode),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ChiefLinkMan",SqlDbType.Int,4,m.ChiefLinkMan),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ActiveFlag",SqlDbType.Int,4,m.ActiveFlag),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@OpenTime",SqlDbType.DateTime,8,m.OpenTime.ToString("yyyy-MM-dd")),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@CloseTime",SqlDbType.DateTime,8,m.CloseTime.ToString("yyyy-MM-dd")),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ClientType",SqlDbType.Int,4,m.ClientType),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Supplier",SqlDbType.Int,4,m.Supplier),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ClientManager",SqlDbType.Int,4,m.ClientManager),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ApproveFlag",SqlDbType.Int,4,m.ApproveFlag),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("InsertTime",SqlDbType.DateTime,8,m.InsertTime),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("InsertStaff",SqlDbType.Int,4,m.InsertStaff),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("UpdateTime",SqlDbType.DateTime,8,m.UpdateTime),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@UpdateStaff",SqlDbType.Int,4,m.UpdateStaff),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@ExtPropertys",SqlDbType.VarChar,8000,m.ExtPropertys)
        
                                        };
            #endregion

            int ret = MCSFramework.DBUtility.SQLDatabase.RunProc(_ProcePrefix + "_Update", patameters);
            return ret;

        }

        public DataTable GetFnaTable(int AccountMonth, int OrganizeCity, int State, int Staff, int RTChannel, int RTType)
        {
            SqlParameter[] patameters = { 
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@AccountMonth",SqlDbType.Int,4,AccountMonth),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@OrganizeCity",SqlDbType.Int,4,OrganizeCity),
                    //MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Level",SqlDbType.Int,4,Level),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@State",SqlDbType.Int,4,State),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@Staff",SqlDbType.Int,4,Staff),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@RTChannel",SqlDbType.Int,4,RTChannel),
                    MCSFramework.DBUtility.SQLDatabase.MakeInParam("@RTType",SqlDbType.Int,4,RTType)
                                        };
            SqlDataReader dr = null;
            MCSFramework.DBUtility.SQLDatabase.RunProc("MCS_FNA.dbo.sp_FNA_FeeApply_GetRTChannelFLFee", patameters, out dr);
            DataTable dt = Tools.ConvertDataReaderToDataTable(dr);
            return dt;
        }

    }
}
